/*
 * Copyright 2008 Nathan C Jones
 * 
 * This file is part of Fivehundred.
 * 
 * Fivehundred is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Fivehundred is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Fivehundred. If not, see <http://www.gnu.org/licenses/>. 
 */

package com.googlecode.fivehundred.model;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;

/**
 * An all-purpose card collection which can be shuffled.
 * 
 * @author Nathan C Jones
 */
public class CardPile extends AbstractCardCollection {

	/** The cards in the pile. */
	private final LinkedList<Card> cards;

	public CardPile() {
		cards = new LinkedList<Card>();
	}

	@Override
	protected Collection<Card> getCards() {
		return cards;
	}

	/**
	 * Get and remove the top card from the card pile.
	 * 
	 * @return the top card from the pile.
	 * @throws EmptyCardPileException if the card pile was empty.
	 */
	public Card removeTop() {
		final Card card = getTop();
		cards.removeLast();
		return card;
	}

	/**
	 * Rearrange the cards in the pile.
	 */
	public void shuffle() {
		Collections.shuffle(cards);
	}

	/**
	 * Get the top card from the card pile. The card will not be removed.
	 * 
	 * @return the top card from the pile.
	 * @throws EmptyCardPileException if the card pile was empty.
	 */
	public Card getTop() {
		if (cards.isEmpty()) {
			throw new EmptyCardPileException();
		}
		return cards.getLast();
	}
}
